package software;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

class Exercise extends ArrayList<BinaryOperation>{
	private static final long serialVersionUID = 1L;
	//生成operationCount个混合算式
	public void generateBinaryExercise(int operationCount) {
		clear();
		BinaryOperation anOperation;
		while(operationCount>0) {
			do {
				anOperation = generateOperation();
			}while(contains(anOperation));
			add(anOperation);
			operationCount--;
		}
	}
	//生成单个混合算式
	private BinaryOperation generateOperation() {
		Random random = new Random();
		int opValue = random.nextInt(2);
		if(opValue == 1)return new AdditionOperation();
		return new SubstractOperation();
	}
	//生成operationCount个加法算式
	public void generateAdditionExercise(int operationCount) {
		clear();
		AdditionOperation anOperation;
		while(operationCount>0) {
			do {
				anOperation = new AdditionOperation();
			}while(contains(anOperation));
			add(anOperation);
			operationCount--;
		}
	}
	//生成operationCount个减法算式
	public void generateSubstractExercise(int operationCount) {
		clear();
		SubstractOperation anOperation;
		while(operationCount>0) {
			do {
				anOperation = new SubstractOperation();
			}while(contains(anOperation));
			add(anOperation);
			operationCount--;
		}
	}
	//将加法算式写到CSV文件file,每行count个
	public void writeCSVAddtionExercise(int count,File file) {
		
		try {
			int tcnt=0;
			if(!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fileWriter);
			
			for(int i=0;i<size()&&tcnt<count;++i) {
				if(get(i).getOperator()=='+') {
					tcnt++;
					bw.write(get(i).toString()+",");
					if(tcnt%5==0)bw.write("\n");
				}
			}
			System.out.println(file.getName()+"成功写入"+tcnt+"个加法算式");
			bw.close();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	//读取加法csv文件csvFile
	public Exercise readCSVAddtionExercise(File csvFile){
		String filename = null;
		filename = csvFile.getPath();
		BufferedReader breader = null;
		Exercise exerciseAdd = new Exercise();
		String stradd="-?[0-9]{1,3} *\\+ *-?[0-9]{1,3}";
		
        try{
            breader = new BufferedReader(new FileReader(filename));
            String line = null;
            while(null!=(line=breader.readLine())){
                String[] lines = line.split(",");
                for(int i=0;i<lines.length;++i) {
                	String temp=lines[i].trim();//去掉首位空格
                	if(temp.matches(stradd)) {
                		String numbers[] = temp.split(" *\\+ *");
                		int left = Integer.parseInt(numbers[0]);
                		int right = Integer.parseInt(numbers[1]);
                		if(left<BinaryOperation.LOWER|
                				left>BinaryOperation.UPPER|
                				right<BinaryOperation.LOWER|
                				right>BinaryOperation.UPPER|
                				left+right>BinaryOperation.UPPER
                				) {
                			System.out.println("字符串："+lines[i]+"不是标准"+BinaryOperation.UPPER+"以内加法算式");
                		}else {
                			AdditionOperation anOperation = new AdditionOperation(left,right);
	                		exerciseAdd.add(anOperation);
                		}
                			
                	}else {
                		System.out.println("字符串："+lines[i]+"不是标准"+BinaryOperation.UPPER+"以内加法算式");
                	}
                }
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
        finally{
            if(breader != null){
                try{
                    breader.close();
                }
                catch(IOException e){
                    System.out.println("Error");
                }
            }
        }
	   
        return exerciseAdd;
	}
	
	//产生number个后加法算式，再将其存入file
	public void writeCSVAddtionExercises(int number, int count,File file) {
		generateAdditionExercise(number);
		writeCSVAddtionExercise(count,file);
	}
	//将减法算式写到CSV文件file,每行count个
	public void writeCSVSubstractExercise(int count,File file) {
		
		try {
			int tcnt=0;
			if(!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fileWriter);
			
			for(int i=0;i<size()&&tcnt<count;++i) {
				if(get(i).getOperator()=='-') {
					tcnt++;
					bw.write(get(i).toString()+",");
					if(tcnt%5==0)bw.write("\n");
				}
			}
			System.out.println(file.getName()+"成功写入"+tcnt+"个减法算式");
			bw.close();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	//读减法习题文件csvFile
	public Exercise readCSVSubstractExercise(File csvFile){
		String filename = null;
		filename = csvFile.getPath();
		BufferedReader breader = null;
		Exercise exerciseSub = new Exercise();
		String strsub="-?[0-9]{1,3} *- *-?[0-9]{1,3}";
		
        try{
            breader = new BufferedReader(new FileReader(filename));
            String line = null;
            while(null!=(line=breader.readLine())){
                String[] lines = line.split(",");
                for(int i=0;i<lines.length;++i) {
                	String temp=lines[i].trim();//去掉首位空格
                	if(temp.matches(strsub)) {
                		String numbers[] = temp.split(" *- *");
                		int left = Integer.parseInt(numbers[0]);
                		int right = Integer.parseInt(numbers[1]);
                		if(left<BinaryOperation.LOWER|
                				left>BinaryOperation.UPPER|
                				right<BinaryOperation.LOWER|
                				right>BinaryOperation.UPPER|
                				left-right<BinaryOperation.LOWER
                				) {
                			System.out.println("字符串："+lines[i]+"不是标准"+BinaryOperation.UPPER+"以内减法算式");
                		}else {
                			SubstractOperation anOperation = new SubstractOperation(left,right);
	                		exerciseSub.add(anOperation);
                		}
                			
                	}else {
                		System.out.println("字符串："+lines[i]+"不是标准"+BinaryOperation.UPPER+"以内减法算式");
                	}
                }
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
        finally{
            if(breader != null){
                try{
                    breader.close();
                }
                catch(IOException e){
                    System.out.println("Error");
                }
            }
        }
	   
        return exerciseSub;
	}
	//产生number个减法算式，再将其存入file
	public void writeCSVSubstractExercises(int number, int count,File file) {
		generateSubstractExercise(number);
		writeCSVSubstractExercise(count,file);
	}
	
	public void writeCSVBinaryExercise(int count,File file) {
		
		try {
			int tcnt=0;
			if(!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fileWriter);
			
			for(int i=0;i<size()&&tcnt<count;++i) {
					tcnt++;
					bw.write(get(i).toString()+",");
					if(tcnt%5==0)bw.write("\n");
			}
			System.out.println(file.getName()+"成功写入"+tcnt+"个算式");
			bw.close();
			fileWriter.close();
		}catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public Exercise readCSVBinaryExercise(File csvFile){
		String filename = null;
		filename = csvFile.getPath();
		BufferedReader breader = null;
		Exercise exerciseBin = new Exercise();
		String stradd="-?[0-9]{1,3} *\\+ *-?[0-9]{1,3}";
		String strsub="-?[0-9]{1,3} *- *-?[0-9]{1,3}";
		
        try{
            breader = new BufferedReader(new FileReader(filename));
            String line = null;
            while(null!=(line=breader.readLine())){
                String[] lines = line.split(",");
                for(int i=0;i<lines.length;++i) {
                	String temp=lines[i].trim();//去掉首位空格
                	if(temp.matches(stradd)) {
                		String numbers[] = temp.split(" *\\+ *");
                		int left = Integer.parseInt(numbers[0]);
                		int right = Integer.parseInt(numbers[1]);
                		if(left<BinaryOperation.LOWER|
                				left>BinaryOperation.UPPER|
                				right<BinaryOperation.LOWER|
                				right>BinaryOperation.UPPER|
                				left+right>BinaryOperation.UPPER
                				) {
                			System.out.println("字符串："+lines[i]+"不是标准"+BinaryOperation.UPPER+"以内加法算式");
                		}else {
                			AdditionOperation anOperation = new AdditionOperation(left,right);
	                		exerciseBin.add(anOperation);
                		}
                			
                	}else if(temp.matches(strsub)) {
                		String numbers[] = temp.split(" *- *");
                		int left = Integer.parseInt(numbers[0]);
                		int right = Integer.parseInt(numbers[1]);
                		if(left<BinaryOperation.LOWER|
                				left>BinaryOperation.UPPER|
                				right<BinaryOperation.LOWER|
                				right>BinaryOperation.UPPER|
                				left-right<BinaryOperation.LOWER
                				) {
                			System.out.println("字符串："+lines[i]+"不是标准"+BinaryOperation.UPPER+"以内减法算式");
                		}else {
                			SubstractOperation anOperation = new SubstractOperation(left,right);
	                		exerciseBin.add(anOperation);
                		}
                			
                	}else {
                		System.out.println("字符串："+lines[i]+"不是标准"+BinaryOperation.UPPER+"以内算式");
                	}
                }
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
        finally{
            if(breader != null){
                try{
                    breader.close();
                }
                catch(IOException e){
                    System.out.println("Error");
                }
            }
        }
	   
        return exerciseBin;
	}
	
	public void writeCSVBinaryExercises(int number, int count,File file) {
		generateBinaryExercise(number);
		writeCSVBinaryExercise(count,file);
	}
	
}
